package org.example.howto;

import com.alibaba.fastjson.JSONObject;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class Java8ForEachWithIndex {
    public static void main(String[] args) {
        listWithIndex();
    }

    static void javaArrayWithIndex(){
        String[] names = {"Java","Node","JavaScript","Rust","Go"};
        List<String> collect = IntStream.range(0,names.length)
                .mapToObj(index -> index + ":" + names[index])
                .collect(Collectors.toList());
        System.err.println(JSONObject.toJSONString(collect,true));
    }

    static void listWithIndex(){
        List<String> names = Arrays.asList("Java","Node","JavaScript","Rust","Go");
        HashMap<Integer,String> collect = names
                .stream()
                .collect(HashMap::new,
                        (map,streamValue) -> map.put(map.size(),streamValue),
                        (map1,map2) ->{});
        System.err.println(JSONObject.toJSONString(collect,true));
    }
}
